import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/home',
    component: () => import('../components/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import('../components/user/Users.vue')
      },
      {
        path: '/roles',
        component: () => import('../components/authority/Roles.vue')
      },
      {
        path: '/rights',
        component: () => import('../components/authority/Rights.vue')
      },
      {
        path: '/goods',
        component: () => import('../components/good/Goods.vue')
      },
      {
        path: '/categories',
        component: () => import('../components/good/Categories.vue')
      },
      {
        path: '/params',
        component: () => import('../components/good/Params.vue')
      },
      {
        path: '/orders',
        component: () => import('../components/order/Orders.vue')
      },
      {
        path: '/reports',
        component: () => import('../components/report/Reports.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else if (sessionStorage.getItem('token')) {
    next()
  } else {
    next('/login')
  }
})

export default router
